import uuid

from flask import request, jsonify, redirect, render_template, session, url_for, g

from models import db
from models.index import User, Photo
from utils.commons.commons import verify_login
from utils.qiniu import upload_qiniu, remove_qiniu
from views import index_blu


# 登录
@index_blu.route('/login',methods=["GET","POST"])
def login():

    type = request.method
    if type == "GET":

        return render_template('login.html')
    else:
        name = request.form.get("name")
        password = request.form.get("password")

        user = db.session.query(User).filter(User.user_name==name,User.user_password==password).first()

        if not user:
            ret = {
                'errno': '0111',
                'errmsg': "登录失败"
            }
            return jsonify(ret)

        session['user_id'] = user.id
        ret = {
            'errno':'0',
            'errmsg':"登录成功"
        }

        return jsonify(ret)

# 照片主页
@index_blu.route('/')
@verify_login
def  index():

    user = g.user
    # 验证登录
    if not user:
        return redirect('/login')
    # 全部的
    images = user.photos

    print(images)

    return  render_template('index.html',images=images)

# 上传页面文件
@index_blu.route('/add_image',methods=["POST","GET"])
def add_image():
    # 验证登录
    user_id = session.get('user_id')
    if not user_id:
        return redirect('/login')
    # 获取请求方式
    type = request.method

    if type == "GET":

        return render_template('contact.html')

    else:

        imags = request.files.getlist("file")

        if not imags:

            ret = {
                "errno":'1001',
                "errmsg":'????',
            }

            return jsonify(ret)

        for image in imags:

            image_type = image.filename[image.filename.rfind("."):]

            # 图片名字
            image_filename = "_xjzx_" + str(uuid.uuid3(uuid.NAMESPACE_URL, image.filename)) + image_type

            url = upload_qiniu(image, image_filename)

            # print(url)  # http://qhq9d7bnb.hn-bkt.clouddn.com/weitezh_图片名  把这个路径存到数据库
            # 保存到数据库
            pro_img = Photo(photo_path=url,user_id=user_id)

            db.session.add(pro_img)

            db.session.commit()

            ret ={
                'errno':'0',
                'errmsg':'上传成功'
            }

    return jsonify(ret)

# 图片详情
@index_blu.route("/details/<int:image_id>")
def details(image_id):

    image = db.session.query(Photo).filter(Photo.id==image_id).first()

    return render_template("blog-post.html",image = image)

# 添加图片描述
@index_blu.route("/add_image_dis",methods=["POST","GET"])
def add_image_dis():

    # 获取数据
    dig_title = request.form.get('dig_title')
    image_dis = request.form.get("image_dis")
    image_id = request.form.get("image_id")

    # 添加到数据库

    image = db.session.query(Photo).filter(Photo.id==image_id).first()

    image.photo_dig_title = dig_title
    image.photo_dig = image_dis

    db.session.commit()

    ret = {"errno":"0",
           "errmsg":"成功"}


    return jsonify(ret)

# 删除图片
@index_blu.route("/delete_image",methods=["POST"])
def delete_image():

    print(111111111111111)
    # 接送参数 要删除的图片的id
    image_id = request.form.get("image_id")

    print(image_id)

    img = db.session.query(Photo).filter(Photo.id == image_id).first()

    result = remove_qiniu(img.photo_path)

    db.session.delete(img)

    db.session.commit()

    if result:

        print("111111111111")
        ret = {"errno":'0',
               "errmsg":"删除成功"}

        return jsonify(ret)

    else:
        ret = {"errno":"0",
               "errmsg":'删除失败'}

        return jsonify(ret)



